package com.sikao.app.ui;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.adapter.CreditRecordAdapter;
import com.sikao.app.bean.CreditRecordBean;
import com.sikao.app.bean.FundRecordBean;
import com.sikao.app.bean.GetListSubmitBean;
import com.sikao.app.dao.CreditRecordModel;
import com.sikao.app.net.HttpApi;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.view.RefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 个人中心信誉记录
 */
public class CreditRecordActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private RefreshListView listView;
	private CreditRecordAdapter adapter;
	private CreditRecordModel creditRecordModel;
	private GetListSubmitBean bean;
	private List<CreditRecordBean.ResBean.ListBean> beans;
	private int page = 1;
	private int allPage;
	private int type;//type = 0,首次加载，1代表刷新，2代表加载更多
	private View noData, noNetwork;//无数据和无网络布局

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_credit_record);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {

	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		listView = (RefreshListView) findViewById(R.id.refresh_listview);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		listView.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				page = 1;
				type = 1;
				getList(false);
			}

			@Override
			public void onLoadMore() {
				page++;
				type = 2;
				if (page > allPage) {
					Toast.makeText(CreditRecordActivity.this, "无更多内容", Toast.LENGTH_SHORT).show();
					listView.onLoadMoreComplete();
				} else {
					getList(false);
				}
			}
		});
	}

	@Override
	protected void init() {
		titleTv.setText("信誉记录");
		backIv.setVisibility(View.VISIBLE);
		bean = new GetListSubmitBean();
		beans = new ArrayList<>();
		adapter = new CreditRecordAdapter(this, beans);
		listView.setAdapter(adapter);
		creditRecordModel = new CreditRecordModel(this, new OnNetBackListener<CreditRecordBean>() {
			@Override
			public void netBack(CreditRecordBean data) {
				if (type == 1) {
					listView.onRefreshComplete();
				}
				if (type == 2) {
					listView.onLoadMoreComplete();
				}
				//无网络
				if (data == null) {
					//清除数据，数据不为空emptyView不会显示
					if (beans.size() != 0) {
						beans.clear();
						adapter.notifyDataSetChanged();
					}
					//如果无数据布局加载过要隐藏掉
					if (noData != null) {
						noData.setVisibility(View.GONE);
					}
					//判断是否加载过无网络布局，避免重复加载显示
					if (noNetwork == null) {
						noNetwork = getLayoutInflater().inflate(R.layout.no_network, null);
						((ViewGroup) listView.getParent()).addView(noNetwork);
						listView.setEmptyView(noNetwork);
						Button button = (Button) noNetwork.findViewById(R.id.btn_request);
						button.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								type = 0;
								page = 1;
								getList(true);
							}
						});
					} else {
						noNetwork.setVisibility(View.VISIBLE);
					}
					return;
				}

				if (data.getStatus().equals("1")) {
					allPage = data.getRes().getAllPage();
					if (type != 2 && beans.size() != 0) {
						beans.clear();
					}
					beans.addAll(data.getRes().getList());
					adapter.notifyDataSetChanged();
				} else {
					//无数据
					if (data.getStatus().equals("0")) {
						//清除数据，数据不为空emptyView不会显示
						if (beans.size() != 0) {
							beans.clear();
							adapter.notifyDataSetChanged();
						}
						//如果无网络布局加载过要隐藏掉
						if (noNetwork != null) {
							noNetwork.setVisibility(View.GONE);
						}
						//判断是否加载过无数据布局，避免重复加载显示
						if (noData == null) {
							noData = getLayoutInflater().inflate(R.layout.content_empty, null);
							((ViewGroup) listView.getParent()).addView(noData);
							listView.setEmptyView(noData);
							noData.setOnClickListener(new View.OnClickListener() {
								@Override
								public void onClick(View v) {
									type = 0;
									page = 1;
									getList(true);
								}
							});
						} else {
							noData.setVisibility(View.VISIBLE);
						}

					} else {
						Toast.makeText(CreditRecordActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		getList(true);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			default:
				break;
		}
	}

	private void getList(boolean isShow) {
		bean.setThisPage(page + "");
		bean.setPageNum(HttpApi.LIST_NUMBER);
		creditRecordModel.request(JSONUtil.toJSON(bean), isShow);
	}
}
